import Vue from 'vue'
import VueRouter from 'vue-router'
import recommend from "../views/recommend.vue";  //同步路由加载
import guidePage from "../components/public/guidePage.vue";  //同步路由加载


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: "/recommend"
  },
  {
    path: "/recommend",
    name: "recommend",
    // 添加路由元信息
    meta: {
      headerNav: true
    },
    component: recommend,
     // 独享路由守卫
    beforeEnter: (to, from, next) => {
      let login = window.localStorage.getItem("login");
      // 判断登录情况
      if(!login){
        next("/login");
        return 
      }else{
        next();
      }
    }
  },
  {
    path: "/guidePage",
    name: "guidePage",
    component: guidePage
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../components/base/login.vue") //懒加载
  },
  {
    path: "/singer",
    name: "singer",
    meta: {
      headerNav: true
    },
    component: () => import("../views/singer.vue")  //懒加载
  },
  {
    path: "/rank",
    name: "rank",
    meta: {
      headerNav: true
    },
    component: () => import("../views/rank.vue")
  },
  {
    path: "/newmv",
    name: "newmv",
    meta: {
      headerNav: true
    },
    component: () => import("../views/newmv.vue")
  },
  {
    path: "/rank/ranklist",
    name: "ranklist",
    component: () => import("../components/rank/ranklist.vue")
  },
  {
    path: "/singer/singers",
    name: "singers",
    component: () => import("../components/base/singers")
  },
  {
    path: "/search",
    name: "search",
    component: () => import("../views/search.vue")
  },
  {
    path: "/collection",
    name: "collection",
    component: () => import("../components/collection.vue")
  },
  {
    path: "/comments",
    name: "comments",
    component: () => import("../components/comments.vue")
  },
  {
    path: "/editMages",
    name: "editMages",
    component: () => import("../components/base/editMages.vue")
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});
router.beforeEach((to,from,next)=>{
  let login = window.localStorage.getItem("login");
  // 登录界面
  if(to.path == "/login"){
    next();
    return;
  }else{
    // 判断登录情况
    if(!login){
      next("/login");
      return;
    }else{
      next();
    }
  }
})

export default router
